package com.hzya.frame.utils.help;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class AES {
	private static Log LOG = LogFactory.getLog(AES.class);
	private static String KEY = "`o7eotan9ws8c:aj";

	public static void main(String[] args) throws Exception {
		System.out.println(
				Decrypt("1e621eae490fffb8e28f5760cb7220a0dd0b0dc9d82f8bfecc2fe64118c53e95", "jaiettghuwk8hqaj"));
		System.out.println(
				Decrypt("82e9e51a1ac8193b2f3c6f5c883a69969311e98f78b64cdf54e8c0c9a6bb37ce", "u47eztkajwp8lq9j"));

		String password = "12345";
		String newPassWord = Encrypt(password);
		System.out.println("加密"+newPassWord);
		String jiemi =Decrypt(newPassWord);
		System.out.println(jiemi);

	}

	public static String Decrypt(String sSrc, String sKey) {
		try {
			if (sKey == null) {
				LOG.error("Key为空null");
				return null;
			}

			if (sKey.length() != 16) {
				LOG.error("Key长度不是16位");
				return null;
			}
			byte[] raw = sKey.getBytes("ASCII");
			SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
			Cipher cipher = Cipher.getInstance("AES");
			cipher.init(2, skeySpec);
			byte[] encrypted1 = hex2byte(sSrc);
			try {
				byte[] original = cipher.doFinal(encrypted1);
				String originalString = new String(original ,"UTF-8");
				return originalString;
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		}
	}
	/**
	 * 解密
	 * @param sSrc
	 * @return
	 */
	public static String Decrypt(String sSrc) {
		return Decrypt(sSrc, KEY);
	}
	/**
	 * 加密
	 * @param sSrc
	 * @return
	 */
	public static String Encrypt(String sSrc) {
		return Encrypt(sSrc, KEY);
	}

	public static String Encrypt(String sSrc, String sKey) {
		try {
			if (sKey == null) {
				LOG.error("Key为空null");
				return null;
			}

			if (sKey.length() != 16) {
				LOG.error("Key长度不是16位");
				return null;
			}
			byte[] raw = sKey.getBytes("ASCII");
			SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
			Cipher cipher = Cipher.getInstance("AES");
			cipher.init(1, skeySpec);
			byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
			return byte2hex(encrypted).toLowerCase();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static byte[] hex2byte(String strhex) {
		if (strhex == null)
			return null;

		int l = strhex.length();
		if (l % 2 != 0)
			return null;

		byte[] b = new byte[l / 2];
		for (int i = 0; i != l / 2; ++i)
			b[i] = (byte) Integer.parseInt(strhex.substring(i * 2, i * 2 + 2), 16);

		return b;
	}

	public static String byte2hex(byte[] b) {

		String stmp = "";
		StringBuffer buffer = new StringBuffer("");
		for (int n = 0; n < b.length; ++n) {
			stmp = Integer.toHexString(b[n] & 0xFF);
			if (stmp.length() == 1) {
 				buffer.append("0" + stmp);
			}else{
				buffer.append(stmp) ;
			}
		}

		return buffer.toString();
	}
}